<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .modal {
            width: 300px;
            min-height: 100px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
            border-radius: 4px;
            position: fixed;
            z-index: 999;
            left: 50%;
            top: 50%;
            transform: translate3d(-50%, -50%, 0);
            background-color: #fff;
        }

        .modal .header {
            line-height: 40px;
            padding: 0 10px;
            position: relative;
            font-size: 20px;
        }

        .modal .header i {
            font-style: normal;
            color: #999;
            position: absolute;
            right: 15px;
            top: -2px;
            cursor: pointer;
        }

        .modal .body {
            text-align: center;
            padding: 10px;
        }

        .modal .footer {
            display: flex;
            justify-content: flex-end;
            padding: 10px;
        }

        .modal .footer a {
            padding: 3px 8px;
            background: #ccc;
            text-decoration: none;
            color: #fff;
            border-radius: 2px;
            margin-right: 10px;
            font-size: 14px;
        }

        .modal .footer a.submit {
            background-color: #369;
        }
    </style>
</head>

<body>
    <button id="delete">删除</button>
    <button id="login">登陆</button>
    <!-- <div class="modal">
        <div class="header">温馨提示<i>x</i></div>
        <div class="body">您没有删除权限操作</div>
    </div> -->

    <script>
        // 要求:利用面向对象实现,不能是普通的面向过程DOM操作

        // 自定义构造函数
        function Modal(title, message) {
            // 自定义属性
            this.title = title;
            this.message = message;
            // 创建一个div,作为属性绑定给this,将来跨域函数使用
            let div = document.createElement('div');
            // 设置类名和内容
            div.className = 'modal';
            div.innerHTML = `
            <div class="header">${this.title}<i>x</i></div>
            <div class="body">${this.message}</div>
            `;
            // 作为属性绑定给this
            this.div = div;
        }
        // 定义原型方法
        Modal.prototype.open = function () {
            if (document.querySelector('.modal') === null) {
                // 要把div添加到body中
                document.body.appendChild(this.div)
                //添加div的时候就把删除div的功能做好,事件绑定上.获取标签,绑定事件
                this.div.querySelector('i').addEventListener('click',()=>{
                    this.close()
                })
            }

        }
        Modal.prototype.close = function () {
            // 要把div从body中删除
            document.body.removeChild(this.div)
        }

        // 测试
        document.querySelector('#delete').addEventListener('click', function () {
            const m1 = new Modal('温馨提示', '删除成功!')
            m1.open()
        })
        document.querySelector('#login').addEventListener('click', function () {
            const m1 = new Modal('友情提示', '登录失败!')
            m1.open()
        })
    </script>
</body>

</html>